﻿@model AddressAttributeModel
@using Nop.Core.Domain.Catalog;
@using Nop.Services

<script>
    $(document).ready(function () {
        bindBootstrapTabSelectEvent('addressattribute-edit');
    });
</script>

<div class="content">
    @Html.ValidationSummary(false)
    @Html.HiddenFor(model => model.Id)
    <div class="form-horizontal">
        <div id="addressattribute-edit" class="nav-tabs-custom">
            <ul class="nav nav-tabs">
                @Html.RenderBootstrapTabHeader("tab-info", @T("Admin.Address.AddressAttributes.Info"), true)
                @Html.RenderBootstrapTabHeader("tab-values", @T("Admin.Address.AddressAttributes.Values"))
            </ul>
            <div class="tab-content">
                @Html.RenderBootstrapTabContent("tab-info", @TabInfo(), true)
                @Html.RenderBootstrapTabContent("tab-values", @TabValues())
            </div>
        </div>
    </div>
</div>

@{
    //custom tabs
    var eventMessage = new AdminTabStripCreated(this.Html, "addressattribute-edit");
    EngineContext.Current.Resolve<IEventPublisher>().Publish(eventMessage);
    foreach (var eventBlock in eventMessage.BlocksToRender)
    {
        @eventBlock
    }
}

@*save selected tab name*@
<input type="hidden" id="selected-tab-name" name="selected-tab-name" value="@(Html.GetSelectedTabName())">

@helper TabInfo()
{
    <div class="panel-group">
        <div class="panel panel-default">
            <div class="panel-body">
                @(Html.LocalizedEditor<AddressAttributeModel, AddressAttributeLocalizedModel>("addressattribute-localized",
                @<div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.NopLabelFor(model => model.Locales[item].Name)
                        </div>
                        <div class="col-md-9">
                            @Html.NopEditorFor(model => model.Locales[item].Name)
                            @Html.ValidationMessageFor(model => model.Locales[item].Name)
                        </div>
                    </div>
                    @Html.HiddenFor(model => model.Locales[item].LanguageId)
                </div>
            ,
                @<div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.NopLabelFor(model => model.Name)
                        </div>
                        <div class="col-md-9">
                            @Html.NopEditorFor(model => model.Name, required: true)
                            @Html.ValidationMessageFor(model => model.Name)
                        </div>
                    </div>
                </div>
                      ))

                <div class="form-group">
                    <div class="col-md-3">
                        @Html.NopLabelFor(model => model.IsRequired)
                    </div>
                    <div class="col-md-9">
                        @Html.NopEditorFor(model => model.IsRequired)
                        @Html.ValidationMessageFor(model => model.IsRequired)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.NopLabelFor(model => model.AttributeControlTypeId)
                    </div>
                    <div class="col-md-9">
                        @{
                            var attributeControlTypes = ((AttributeControlType)Model.AttributeControlTypeId)
                                .ToSelectList(valuesToExclude:
                                    //these attributes don't support some control types
                                    new[] { (int)AttributeControlType.FileUpload, (int)AttributeControlType.Datepicker, (int)AttributeControlType.ColorSquares, (int)AttributeControlType.ImageSquares });
                        }
                        @Html.NopDropDownListFor(model => model.AttributeControlTypeId, attributeControlTypes)
                        @Html.ValidationMessageFor(model => model.AttributeControlTypeId)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.NopLabelFor(model => model.DisplayOrder)
                    </div>
                    <div class="col-md-9">
                        @Html.NopEditorFor(model => model.DisplayOrder)
                        @Html.ValidationMessageFor(model => model.DisplayOrder)
                    </div>
                </div>
            </div>
        </div>
    </div>
}

@helper TabValues()
{
    <div class="panel-group">
        <div class="panel panel-default">
            @if (Model.Id > 0)
            {
                /*TODO display the following warning if values are not supported by selected attribute control type
                'Values are not required for this attribute control type'
                */
                <div class="panel-body">
                    <div id="addressattributevalues-grid"></div>

                    <script>
                        $(document).ready(function () {
                            $("#addressattributevalues-grid").kendoGrid({
                                dataSource: {
                                    type: "json",
                                    transport: {
                                        read: {
                                            url: "@Html.Raw(Url.Action("ValueList", "AddressAttribute", new { addressAttributeId = Model.Id }))",
                                            type: "POST",
                                            dataType: "json",
                                            data: addAntiForgeryToken
                                        },
                                        destroy: {
                                            url: "@Html.Raw(Url.Action("ValueDelete", "AddressAttribute"))",
                                            type: "POST",
                                            dataType: "json",
                                            data: addAntiForgeryToken
                                        }
                                    },
                                    schema: {
                                        data: "Data",
                                        total: "Total",
                                        errors: "Errors",
                                        model: {
                                            id: "Id"
                                        }
                                    },
                                    error: function (e) {
                                        display_kendoui_grid_error(e);
                                        // Cancel the changes
                                        this.cancelChanges();
                                    },
                                    serverPaging: true,
                                    serverFiltering: true,
                                    serverSorting: true
                                },
                                pageable: {
                                    refresh: true,
                                    numeric: false,
                                    previousNext: false,
                                    info: false,
                                    @Html.Partial("_GridPagerMessages")
                                },
                                editable: {
                                    confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                    mode: "inline"
                                },
                                scrollable: false,
                                columns: [{
                                    field: "Name",
                                    title: "@T("Admin.Address.AddressAttributes.Values.Fields.Name")"
                                }, {
                                    field: "IsPreSelected",
                                    title: "@T("Admin.Address.AddressAttributes.Values.Fields.IsPreSelected")",
                                    width: 100,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:center" },
                                    template: '# if(IsPreSelected) {# <i class="fa fa-check true-icon"></i> #} else {# <i class="fa fa-close false-icon"></i> #} #'
                                }, {
                                    field: "DisplayOrder",
                                    title: "@T("Admin.Address.AddressAttributes.Values.Fields.DisplayOrder")",
                                    width: 100
                                }, {
                                    field: "Id",
                                    title: "@T("Admin.Common.Edit")",
                                    width: 100,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:center" },
                                    template: "<button value='@T("Admin.Common.Edit")' onclick=\"javascript:OpenWindow('@Url.Content("~/Admin/AddressAttribute/ValueEditPopup/")#=Id#?btnId=btnRefresh&formId=addressattribute-form', 800, 400, true); return false;\" class='btn btn-default'><i class=\"fa fa-pencil\"></i>@T("Admin.Common.Edit")</button>"
                                }, {
                                    command: { name: "destroy", text: "@T("Admin.Common.Delete")" },
                                    title: "@T("Admin.Common.Delete")",
                                    width: 100
                                }]
                            });
                        });
                    </script>
                </div>
                <div class="panel-footer">
                    <button type="submit" id="btnAddNewValue" onclick="javascript:OpenWindow('@(Url.Action("ValueCreatePopup", "AddressAttribute", new {addressAttributeId = Model.Id, btnId = "btnRefresh", formId = "addressattribute-form"}))', 800, 400, true); return false;" class="btn btn-primary">
                        @T("Admin.Address.AddressAttributes.Values.AddNew")
                    </button>
                    <input type="submit" id="btnRefresh" style="display: none" />
                    <script type="text/javascript">
                        $(document).ready(function () {
                            $('#btnRefresh').click(function () {
                                //refresh grid
                                var grid = $("#addressattributevalues-grid").data('kendoGrid');
                                grid.dataSource.read();

                                //return false to don't reload a page
                                return false;
                            });
                        });
                    </script>
                </div>
            }
            else
            {
                <div class="panel-body">
                    @T("Admin.Address.AddressAttributes.Values.SaveBeforeEdit")
                </div>
            }
        </div>
    </div>
}
